﻿Imports Microsoft.ApplicationBlocks.Data

Namespace DAL
    Public Class AdminProvider
        Inherits DataAccess
        Private util As New Core.Utilities.Common()

        Public Sub New()

        End Sub

        Public Sub Create(ByVal objAdmin As Core.BLL.AdminClass)
            Dim param(5) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@UserName", SqlDbType.NVarChar, 100)
            param(0).Value = util.NoHTML(objAdmin.UserName)
            param(1) = New SqlClient.SqlParameter("@Password", SqlDbType.NVarChar, 450)
            param(1).Value = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(objAdmin.Password, "MD5")
            param(2) = New SqlClient.SqlParameter("@Email", SqlDbType.VarChar, 50)
            param(2).Value = objAdmin.Email
            param(3) = New SqlClient.SqlParameter("@Permission", SqlDbType.Int, 4)
            param(3).Value = objAdmin.Permission
            param(4) = New SqlClient.SqlParameter("@Status", SqlDbType.Int, 4)
            param(4).Value = objAdmin.Status

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "tbl_Admin_Create", param)
        End Sub

        Public Sub Delete(ByVal objAdmin As Core.BLL.AdminClass)
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@UserID", SqlDbType.Int, 4)
            param(0).Value = objAdmin.UserID

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "tbl_Admin_Delete", param)
        End Sub

        Public Sub Update(ByVal objAdmin As Core.BLL.AdminClass)
            Dim param(5) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@UserID", SqlDbType.NVarChar, 450)
            param(0).Value = objAdmin.UserID
            param(1) = New SqlClient.SqlParameter("@UserName", SqlDbType.NVarChar, 100)
            param(1).Value = util.NoHTML(objAdmin.UserName)
            param(2) = New SqlClient.SqlParameter("@Email", SqlDbType.VarChar, 50)
            param(2).Value = objAdmin.Email
            param(3) = New SqlClient.SqlParameter("@Permission", SqlDbType.Int, 4)
            param(3).Value = objAdmin.Permission
            param(4) = New SqlClient.SqlParameter("@Status", SqlDbType.Int, 4)
            param(4).Value = objAdmin.Status

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "tbl_Admin_Update", param)
        End Sub

        Public Function GetByID(ByVal objAdmin As Core.BLL.AdminClass) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@UserID", SqlDbType.Int, 4)
            param(0).Value = objAdmin.UserID
            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "tbl_Admin_Select", param)
        End Function

        Public Function Login(ByVal objAdmin As Core.BLL.AdminClass) As SqlClient.SqlDataReader
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@UserName", SqlDbType.NVarChar, 50)
            param(0).Value = util.NoHTML(objAdmin.UserName)
            param(1) = New SqlClient.SqlParameter("@Password", SqlDbType.VarChar, 150)
            param(1).Value = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(objAdmin.Password, "MD5")

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "tbl_Admin_Login", param)
        End Function

        Public Function GetAll(ByVal objAdmin As Core.BLL.AdminClass) As DataSet
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@Status", SqlDbType.Int, 4)
            param(0).Value = objAdmin.Status
            param(1) = New SqlClient.SqlParameter("@Permission", SqlDbType.Int, 4)
            param(1).Value = objAdmin.Permission

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "tbl_Admin_GetAll", param)
        End Function

        Public Function ResetPassword(ByVal objAdmin As Core.BLL.AdminClass) As SqlClient.SqlDataReader
            Dim param(3) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@NewPassword", SqlDbType.VarChar, 150)
            param(0).Value = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(objAdmin.Password, "MD5")
            param(1) = New SqlClient.SqlParameter("@UserID", SqlDbType.VarChar, 150)
            param(1).Value = objAdmin.UserID
            param(2) = New SqlClient.SqlParameter("@Email", SqlDbType.VarChar, 30)
            param(2).Value = util.NoHTML(objAdmin.Email)

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "tbl_Admin_ResetPassword", param)
        End Function

        Public Function CheckEmailAvaiable(ByVal objUser As Core.BLL.AdminClass) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@Email", SqlDbType.VarChar, 30)
            param(0).Value = objUser.Email

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "tbl_Admin_CheckEmail", param)
        End Function

        Public Function GetUserByEmail(ByVal objUser As Core.BLL.AdminClass) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@Email", SqlDbType.VarChar, 30)
            param(0).Value = objUser.Email

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "tbl_Admin_GetUserByEmail", param)
        End Function

        Public Function CheckExitsAdmin(ByVal objUser As Core.BLL.AdminClass) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@UserName", SqlDbType.VarChar, 30)
            param(0).Value = objUser.UserName
            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "tbl_Admin_Exits", param)
        End Function
    End Class

End Namespace